<?php
class Changepass_Controller extends Template_Controller {

	public $template = 'client/index';
	
    public function __construct()
    {
        parent::__construct();
		 //Css
        $this->template->css = $this->site['theme_url'].'login/login.css';
        //View
        $this->get_view();
    }
    
    public function index()
    {	
        $this->template->content = new View('mypage/frm_changepass');
		
		if($this->session->get('error_msg'))
			$this->template->content->error_msg = $this->session->get('error_msg');
		if($this->session->get('success_msg'))
		$this->template->content->success_msg = $this->session->get('success_msg');
		if(!$this->login_model->get_login(1))
		{
			url::redirect('login');
		}		
    }
    
	function _get_record()
    {
    	$form = array
	    (
	        'txt_oldpassword' => '',
	        'txt_newpassword' => '',
	        'txt_confirmpassword' => '',	       
	    );
	    
	    $errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_oldpassword','required');
			$post->add_rules('txt_newpassword','required');
			$post->add_rules('txt_confirmpassword','required');
						
			if($post->validate())
 			{
 				if( $this->input->post('txt_newpassword')!= $this->input->post('txt_confirmpassword')){
					$error_msg = Kohana::lang('changepass_lang.msg_confirmpass');
					$this->session->set_flash('error_msg',$error_msg);
					url::redirect('changepass');
				    die();
				}
				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('changepass_validation'));
				$error_msg = '';
				foreach($errors as $id => $name) 
					if($name) $error_msg .= '<br>'.$name;				
				$this->session->set_flash('error_msg',$error_msg);
				
				url::redirect('changepass');
				die();
			}
        }
    }
    
	public function _exist_user($id, $pass)
	{
	   $result = ORM::factory('customer')->where(array('customers_id' => $id,'customers_pass' => $pass))->find();
	   $user_exists=$result->customers_username;
	   return $user_exists;
	}
	
	public function sm()
    {
    	$info=$this->login_model->get_login(1);
		$hd_id=$info['id'];
		$oldpassword = $this->input->post('txt_oldpassword');
		$record = $this->_get_record();
    	$exist_user = $this->_exist_user($hd_id,$oldpassword);
		
		if(!empty($exist_user))
    	{
    		$result = ORM::factory('customer',$hd_id);
			
			$result->customers_pass = $record['txt_newpassword'];
			$result->save();
			
			$this->session->set_flash('success_msg',Kohana::lang('changepass_lang.msg_successful'));	
			url::redirect('changepass');
    	} else {
            $this->session->set_flash('success_msg',Kohana::lang('changepass_lang.msg_oldpass'));	
	    	url::redirect('changepass');
			die();
		}
    }
}
?>